import * as mobx from 'mobx';
import { ListStore } from '../listStore';
import { WeaTableNew } from 'comsMobx';
import { WeaForm } from 'comsMobx';
import {message} from 'antd';
const { TableStore } = WeaTableNew;
import * as WORKFLOW_CUSTOMFORM from '../../apis/customReportForm';
import { WeaLocaleProvider,WeaTools } from 'ecCom';

const { observable, action } = mobx;
const getLabel = WeaLocaleProvider.getLabel;
const Base64 = WeaTools.Base64;
const initResultPramas = {
    fieldId:[{}],
    operate:"1",
    fieldValue:[{}]
}

export class WFCusReportFormStore extends ListStore{

    @observable tab = 0;     //标志tabs
    @observable shareTable = new TableStore();     //共享table
    @observable reportid = '';   //记录行按钮的id


    // TODO_lj========================
    @observable isShowFormulaSelDialog = true;   //是否显示条件设置弹窗
    formulaSelDialogPrams1 = {};   //要传入浏览按钮的参数
    @observable formulaSelDialogPrams2 = {};   //要传入浏览按钮的参数
    resultPrams = initResultPramas //点击确定后把选中的数据封装到这个对象
    // TODO_lj========================ENT TODO


    constructor () {
        super();
        //this.title = getLabel(382259, '报表分析');
        this.switchTabs = this.switchTabs.bind(this);
        this.initDatas = this.initDatas.bind(this);
        this.doSearch = this.doSearch.bind(this);
        this.queryCondition = this.queryCondition.bind(this);
        this.batchDelete = this.batchDelete.bind(this);
        this.reload = this.reload.bind(this);
        this.saveAsOtherSave = this.saveAsOtherSave.bind(this);
        this.setReportid = this.setReportid.bind(this);
        this.setConditionByTree = this.setConditionByTree.bind(this);
    }

    @action

    initDatas (params = {}) {
        this.title = getLabel(382259, '报表分析');
        if(params.tabTitle){
            this.title = Base64.decode(decodeURIComponent(params.tabTitle));
        }
        this.form = new WeaForm();
        const tab = this.tab;
        let newParama = {...params,tab};
        WORKFLOW_CUSTOMFORM.getCustomReportHighSearch(newParama).then((datas)=>{
            this.condition = datas.conditions;
            this.form.initFormFields(datas.conditions);
            if(params.jumpTab){
              this.switchTabs(params.jumpTab);
            }
        })
    }

    doSearch (params = {}) {
        this.loading = true;
        const searchParamsAd = this.form.getFormParams();
        const tab = this.tab;
        const newParams = {...searchParamsAd,...params,tab};
        WORKFLOW_CUSTOMFORM.getCustomReportList(newParams).then((datas)=>{
            const { sessionkey } = datas;
            this.tableStore.getDatas(sessionkey, params.current || 1);
            this.loading = false;
        });
    }

    //报表删除
    batchDelete (reportids = '') {
        message.loading(getLabel(84024,'正在删除数据，请稍候...'),0);
        WORKFLOW_CUSTOMFORM.getCRDelete({reportids}).then((datas)=>{
            if(datas.success){
                message.destroy();
                message.success(getLabel(20461,'删除成功'));
                this.reload();
            }else {
                message.destroy();
                message.error(getLabel(83473,'删除失败'));
            }
        });
    }

    //报表另存为
    saveAsOtherSave (params = {}) {
        message.loading(getLabel(23278,'正在保存，请稍后...'),0);
        WORKFLOW_CUSTOMFORM.getCRSaveAsOther(params).then((datas)=>{
            if(datas.success){
                message.destroy();
                message.success(getLabel(83551,'保存成功'));
                this.reload();
            }else {
                message.destroy();
                message.error(getLabel(84544,'保存失败'));
            }
        })
    }

    switchTabs (tab = 0) {
        this.tab = tab;
        this.initDatas();
        this.doSearch();
    }

    reload () {
        const current = this.tableStore.current;
        this.doSearch({current});
    }

    setReportid (reportid = '') {
        this.reportid = reportid;
    }

    //打开查询条件
    queryCondition (params = {}) {
        WORKFLOW_CUSTOMFORM.getCRQuery(params).then((datas)=>{
        })
    }

    setConditionByTree () {
        return ;
    }

    clearStatus () {
        this.showSearchAd = false;
        this.clearFormFields();
        this.tab = 0;
        this.reportid = '';
        this.title = getLabel(382259, '报表分析');
        this.resetTable();
    }

    @action('修改store中的属性')
    setState = (state = {}) => {
        const keys = Object.keys(state);
        keys.forEach(key => {
            this[key] = state[key];
        })
    }

    @action("点击显示条件设置弹窗") //TODO_lj
    initFormulaSelection = () => {
      this.resultPrams = initResultPramas;

    }

    @action("修改ResultPrams的值")
    setResultPrams = (key,val) => {
        this.resultPrams[key] = val
    }
}